<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size:16px">
      <el-breadcrumb-item :to="{ path: '/sys-admin/index' }">
        <i class="el-icon-s-promotion"></i>首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>
        管理员列表
      </el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="ID" width="60" align="center"></el-table-column>
      <el-table-column prop="username" label="用户名" width="120" align="center"></el-table-column>
      <el-table-column prop="nickname" label="昵称" width="180" align="center"></el-table-column>
      <el-table-column prop="phone" label="手机号" width="180" align="center"></el-table-column>
      <el-table-column prop="email" label="电子邮件" width="150" align="center"></el-table-column>
      <el-table-column prop="description" label="简介" width="180" align="center"></el-table-column>
      <el-table-column prop="enable" label="是否启用" width="80" align="center">
        <template slot-scope="scope">
          <el-switch
              @change="handleChangeEnable(scope.row)"
              v-model="scope.row.enable"
              :active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#999">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" circle
                     @click="handleEdit(scope.row.id)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" circle
                     @click="handleDelete(scope.row.id)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: []
    }
  },
  methods: {
    handleEdit(id) {
      alert('你点击了id=' + id + '的管理员数据的【编辑】按钮！');
    },
    handleDelete(id) {
      // alert('你点击了id=' + id + '的管理员数据的【删除】按钮！');
      let url = 'http://localhost:9081/admins/' + id + '/delete';
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state != 20000) {
          this.$message.error(responseBody.message);
        }
        this.loadAdminList();
      });
    },
    handleChangeEnable(admin) {
      // alert('你点击了id=' + admin.id + '的管理员数据的【启用】开关！新的状态为：' + admin.enable);
      let enableText = ['禁用', '启用'];
      let url = 'http://localhost:9081/admins/' + admin.id;
      if (admin.enable == 1) {
        url += '/enable';
      } else {
        url += '/disable';
      }
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          let message = '将【' + admin.username + '】的账号状态改为【' + enableText[admin.enable] + '】成功';
          console.log(message);
          this.$message({
            message: message,
            type: 'success'
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    loadAdminList() {
      console.log('loadAdminList');
      let jwt = localStorage.getItem('jwt');
      console.log('在loadStorage中的JWT数据：' + jwt);
      console.log('准备从服务器获取管理员列表……');
      let url = 'http://localhost:9081/admins';
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then(response => {
        console.log('服务端响应结果:')
        console.log(response);
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.tableData = responseBody.data;
        }else {
          this.$message.error(responseBody.message);
        }
      })
    }
  },
  created() {
    console.log('执行生命周期方法：created');
  },
  mounted() {
    console.log('执行生命周期方法：mounted');
    this.loadAdminList();
  }
}
</script>

<style>

</style>